package com.grape.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grape.utils.StringUtils;
import com.grape.vo.BaseVo;
import com.grape.wrapper.BaseWrapper;
import com.grape.domain.SysDictItem;
import com.grape.mapper.SysDictItemMapper;
import com.grape.service.ISysDictItemService;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * 字典数据表 服务实现类
 *
 * @author  putao
 * @email   2628279194@qq.com
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements ISysDictItemService {

    private final SysDictItemMapper sysDictItemMapper;

    /**
     * 分页
     * @param baseVo 统一分页对象
     * @return 字典数据表集合
     */
    @Override
    public List<SysDictItem> listData(BaseVo baseVo) {
        QueryWrapper<SysDictItem> wrapper = new BaseWrapper<SysDictItem>().init();
        wrapper.like(Strings.isNotBlank(baseVo.getSearch()), "name", baseVo.getSearch());
        wrapper.eq(StringUtils.isNotBlank(baseVo.getColumnName())
                && StringUtils.isNotBlank(baseVo.getSelect()), baseVo.getColumnName(), baseVo.getSelect());
        orderByModifyTimeDesc(wrapper);
        return this.list(wrapper);
    }

    /**
     * 修改
     * @param sysDictItem 字典数据表对象
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void updateData(SysDictItem sysDictItem) {
        this.baseUpdate(sysDictItem);
    }

    /**
     * 新增
     * @param sysDictItem 字典数据表对象
     * @return 字典数据表对象
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public SysDictItem insertData(SysDictItem sysDictItem) {
        return this.baseInsert(sysDictItem);
    }

    /**
     * 删除
     * @param id    单据id
     * @return      删除条数
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public int deleteData(String id) {
        return this.baseDelete(id);
    }

    /**
     * 根据id查询详情
     * @param id    字典数据
     * @return      字典数据对象
     */
    @Override
    public SysDictItem detail(String id) {
        return this.getById(id);
    }
}
